<?php
require_once(APP_LIB_DIR . 'MessagingModel.class.php');
include_once(APP_LIB_DIR . 'JSON.php');

Class MessagingAction Extends Action {
    function getDefaultView(&$controller, &$request, &$user) {
        return VIEW_INPUT;
    }

    function getRequestMethods() {
        return REQ_POST | REQ_GET;
    }

    function IsSecure() {
        return true;
    }

    function getPrivilege (&$controller, &$request, &$user) {
        return Array('MESSAGING');
    }

    function scheduler($controller, $request, $user) {
        $mm = new MessagingModel('Messaging');
        $auth = $user->getAttribute('auth');

        $message = $request->getParameter('message');
        $cat = $request->getParameter('list_cat');
        $smsc = $request->getParameter('smsc');
        $listbox = $request->getParameter('ListBox2');
        $smsc = ($smsc=='auto') ? false : $smsc;
        $ids = implode(array_unique($listbox),',');

        $dt['message']     = $message;
        $dt['by']          = $auth['username'];
        $dt['smsc']        = $smsc;
        $dt['status']        = 'pending';
        $dt['member_ids']  = ($cat == 'list_contacts') ? $ids : "";
        $dt['group_ids']   = ($cat == 'list_groups') ? $ids : "";
        $dt['send_on']     = date("Y-m-d H:i:s",strtotime($request->getParameter('send_date')));

        $mid = $mm->insert('scheduler', $dt);
        $controller->redirect('?module='.DEFAULT_MODULE.'&action=Messaging');
    }

    function sendsms($controller, $request, $user) {
        $mm = new MessagingModel('Messaging');
        $auth = $user->getAttribute('auth');

        $message = $request->getParameter('message');
        $cat = $request->getParameter('list_cat');
        $smsc = $request->getParameter('smsc');
        $listbox = $request->getParameter('ListBox2');
        $smsc = ($smsc=='auto') ? false : $smsc;

        if ($listbox && $message && $cat == 'list_contacts') {
            $ids = implode(array_unique($listbox),',');
            $dt['message']     = $message;
            $dt['by'] = $auth['username'];
            $mid = $mm->insert('messages', $dt);
            foreach($mm->get_msisdn($ids) as $member) {
                $mm->send_group($member['msisdn'], $mid,$smsc,$message,$member['id']);
            }
            $controller->redirect('?module='.DEFAULT_MODULE.'&action=Messaging');
        }
        elseif ($listbox && $message && $cat == 'list_groups') {
            $gids = implode(array_unique($listbox),',');
            $members = $mm->group_members($gids);
            $dt['message']     = $message;
            $dt['by'] = $auth['username'];
            $id = $mm->insert('messages', $dt);
            foreach($members as $member) {
                //$smsc = ($smsc) ? $smsc :$member['type'];
                $smsc = ($smsc=='auto') ? false : $smsc;
                $mm->send_group($member['msisdn'], $id,$smsc,$message, $member['mid'], $member['gid']);
            }
            $controller->redirect('?module='.DEFAULT_MODULE.'&action=Messaging');
        }
    }

    function execute(&$controller, &$request, &$user) {
        $auth = $user->getAttribute('auth');
        $GLOBALS['login'] = $auth['username'];
        $GLOBALS['user_id'] = $auth['id'];
        $GLOBALS['user_privilege'] = $auth['privilege'];
        $mm = new MessagingModel('Messaging');
        global $config;
        $mm->select_db($config['dbname']);

        if($request->getParameter('send') == 'send_now') {
            $this->sendsms($controller,$request,$user);
        }
        elseif($request->getParameter('send') == 'send_sched') {
            $this->scheduler($controller,$request,$user);
        }
        elseif($request->getParameter('resend_msg') > 1) {
            //$mm = new MessagingModel('Messaging');
            $list=$mm->get_failed($request->getParameter('resend_msg'));
            $mm->update_failed($request->getParameter('resend_msg'));
            foreach($list as $member) {
                $mm->send_group($member['msisdn'], $member['message_id'],$member['smsc'],$member['message'],$member['member_id'], $member['group_id']);
            }
            $controller->redirect('?module='.DEFAULT_MODULE.'&action=Messaging');
        }
        elseif($request->getParameter('change_number')) {
            $mm = new MessagingModel('Messaging');
            $stud_id = $request->getParameter('change_number');
            $smsc = $request->getParameter('smsc');
            $smsc = ($smsc=='auto') ? false : $smsc;
            $type = $request->getParameter('type');
            $mm->select_db($config['enrollment_db']);
            if ($type=='student') {
                $stud_info= $mm->get_studinfo($stud_id);
                $mm->select_db($config['dbname']);
                $mem_info= $mm->get_meminfo($stud_info['stud_no']);
            }
            elseif($type=='employee') {
                $stud_info= $mm->get_empinfo($stud_id);
                $mm->select_db($config['dbname']);
                $mem_info= $mm->get_meminfo($stud_info['employee_no']);
            }
            $getmsg= $mm->get_msg();
            $mem_info['group_id']='';

            $mm->send_sms($mem_info['msisdn'], $getmsg['name'],$smsc,$GLOBALS['login'],$mem_info['id'],$mem_info['group_id']);

# echo "Stud NO:" . $stud_info['stud_no']. "," . "First Name:" . $stud_info['fname'];
# echo "Mem ID:" . $mem_info['id']. "," . "Mem Stud No:" . $mem_info['stud_no'] . "Mem First Name:" . $mem_info['firstname'];
            return VIEW_NONE;

        }
        elseif($request->getParameter('paid_notifs')) {
            $mm = new MessagingModel('Messaging');
            $stud_id = $request->getParameter('paid_notifs');
            $smsc = $request->getParameter('smsc');
            $smsc = ($smsc=='auto') ? false : $smsc;
            $msg= $mm->paid_notifs_msg();
            $mm->select_db($config['enrollment_db']);
            $stud_info= $mm->get_studinfo($stud_id);
            $mm->select_db($config['dbname']);
            $mem_info= $mm->get_meminfo($stud_info['stud_no']);
            $mm->send_sms($mem_info['msisdn'], $msg,$smsc,$GLOBALS['login'],$mem_info['id']);
            return VIEW_NONE;
        }
        return VIEW_INPUT;
    }
    function lookup($what,$p1,$p2) {
        $mm = new MessagingModel('Messaging');
        $json   = new Services_JSON();
        switch($what) {
        case 'groups':
            return $json->encode($mm->groups($p1,$p2));
            break;
        case 'contacts':
            return $json->encode($mm->contacts($p1,$p2));
            break;
        default:
            return $json->encode($mm->groups());
            break;
        }
    }
}
?>
